import time, threading

lock = threading.Lock()

class Account:
    def __init__(self, balance):
        self.balance = balance


def draw(account, amount):
    if account.balance >= amount:
        time.sleep(0.1) # 加这句必出问题, sleep会切线程
        print(f'current balance: {account.balance}', f'withdraw: {amount}')
        account.balance -= amount
        print(f'余额: {account.balance}')
    else:
        print('not enough balance')

def draw2(account, amount):
    with lock:
        if account.balance >= amount:
            time.sleep(0.1)  # 加这句必出问题, sleep会切线程
            print(f'current balance: {account.balance}', f'withdraw: {amount}')
            account.balance -= amount
            print(f'余额: {account.balance}')
        else:
            print('not enough balance')

# 用draw及draw2的区别
if __name__ == '__main__':
    account = Account(100)
    import threading
    ta = threading.Thread(target=draw2, args=(account, 80))
    tb = threading.Thread(target=draw2, args=(account, 80))
    ta.start()
    tb.start()
